package com.w3c.pragmatic.controller;

import com.w3c.pragmatic.domain.Response;
import com.w3c.pragmatic.mongo.FileTemplate;
import com.w3c.pragmatic.mongo.FileTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * mongodb 前端控制器
 *
 * @author wangdong
 * @since 2025-02-20
 */
@RestController
@RequestMapping("/mongo")
@Api(tags = "mongodb控制层MongoDbController")
public class MongoDbController {

  @Resource private FileTemplateService fileTemplateService;

  /**
   * 保存模板数据
   *
   * @return String 描述
   */
  @ApiOperation(value = "保存模板数据", httpMethod = "POST")
  @RequestMapping(
      value = "/save",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<FileTemplate> saveTemplate(@RequestBody FileTemplate fileTemplate) {
    FileTemplate template = fileTemplateService.saveTemplate(fileTemplate);
    return Response.ok(template, "保存成功");
  }

  /**
   * 查找并编辑模板
   *
   * @return String 描述
   */
  @ApiOperation(value = "查找并编辑模板", httpMethod = "POST")
  @RequestMapping(
      value = "/findAndUpdate",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<FileTemplate> findAndUpdateTemplate(@RequestBody FileTemplate fileTemplate) {
    FileTemplate template = fileTemplateService.findAndModify(fileTemplate);
    return Response.ok(template, "查找并编辑模板成功");
  }

  /**
   * 按条件批量编辑
   *
   * @return String 描述
   */
  @ApiOperation(value = "按条件批量编辑模板", httpMethod = "POST")
  @RequestMapping(
      value = "/updateMulti",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> updateMultiTemplate(@RequestBody FileTemplate fileTemplate) {
    fileTemplateService.updateMulti(fileTemplate);
    return Response.ok("按条件批量编辑模板成功");
  }

  /**
   * 查询全量数据
   *
   * @return String 列表
   */
  @ApiOperation(value = "查询全量数据", httpMethod = "POST")
  @RequestMapping(
      value = "/findAll",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<List<FileTemplate>> findAll() {
    return Response.ok(fileTemplateService.findAll());
  }

  /**
   * 根据主键id查询
   *
   * @return String 对象
   */
  @ApiOperation(value = "根据主键id查询", httpMethod = "POST")
  @RequestMapping(
      value = "/getById/{id}",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<FileTemplate> getById(@PathVariable Long id) {
    return Response.ok(fileTemplateService.getById(id));
  }

  /**
   * 根据其他唯一条件查询
   *
   * @return String 对象
   */
  @ApiOperation(value = "根据其他唯一条件查询", httpMethod = "POST")
  @RequestMapping(
      value = "/getByCondition",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<FileTemplate> getByCondition(@RequestBody FileTemplate fileTemplate) {
    return Response.ok(
        fileTemplateService.getByUnique(
            fileTemplate.getTemplateType(), fileTemplate.getTemplateCode()));
  }

  /**
   * 根据条件删除
   *
   * @return String 对象
   */
  @ApiOperation(value = "根据条件删除", httpMethod = "POST")
  @RequestMapping(
          value = "/delByCondition",
          method = RequestMethod.POST,
          produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> delByCondition(@RequestBody FileTemplate fileTemplate) {
    fileTemplateService.removeByUnique(
            fileTemplate.getTemplateType(), fileTemplate.getTemplateCode());
    return Response.ok("删除成功");
  }
}
